package com.wcj.linktable;


/**
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 *
 * 请你将两个数相加，并以相同形式返回一个表示和的链表。
 *
 * 你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
 */
public class 两数相加 {


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null){
            return l2;
        }else if (l2 == null){
            return l1;
        }
        ListNode resultHead = null;
        ListNode resultTail = null;
        ListNode point1 = l1;
        ListNode point2 = l2;
        int addSize = 0;
        while (point1 != null && point2 != null){
            int sum = point1.val + point2.val + addSize;
            if (sum >= 10){
                addSize = 1;
            }else {
                addSize = 0;
            }
            sum = sum % 10;
            if (resultHead == null){
                resultHead = resultTail = new ListNode(sum);
            }else {
                resultTail.next = new ListNode(sum);
                resultTail = resultTail.next;
            }
            point1 = point1.next;
            point2 = point2.next;
        }
        while (point1 != null){
            int sum = point1.val + addSize;
            if (sum >= 10){
                addSize = 1;
            }else {
                addSize = 0;
            }
            resultTail.next = new ListNode(sum % 10);
            resultTail = resultTail.next;
            point1 = point1.next;
        }
        while (point2 != null){
            int sum = point2.val + addSize;
            if (sum >= 10){
                addSize = 1;
            }else {
                addSize = 0;
            }
            resultTail.next = new ListNode(sum % 10);
            resultTail = resultTail.next;
            point2 = point2.next;
        }
        if (addSize != 0){
            resultTail.next = new ListNode(addSize);
        }
        return resultHead;
    }

    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        if (l1 == null){
            return l2;
        }else if (l2 == null){
            return l1;
        }
        ListNode resultHead = null;
        ListNode resultTail = null;
        ListNode point1 = l1;
        ListNode point2 = l2;
        int addSize = 0;
        while (point1 != null || point2 != null){
            int sum = (point1==null?0:point1.val) + (point2==null?0:point2.val)+addSize;
            if (sum >= 10){
                addSize = 1;
            }else {
                addSize = 0;
            }
            if (resultHead == null){
                resultHead = resultTail = new ListNode(sum % 10);
            }else {
                resultTail.next = new ListNode(sum % 10);
                resultTail = resultTail.next;
            }
            if (point1 != null){
                point1 = point1.next;
            }
            if (point2 != null){
                point2 = point2.next;
            }
        }
        if (addSize != 0){
            resultTail.next = new ListNode(addSize);
        }
        return resultHead;
    }
}
